package com.listviewre;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;

import com.bumptech.glide.Glide;

import org.json.JSONArray;
import org.json.JSONObject;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private  PullToRefreshView pullToRefreshView=null;
    private ListView listView=null;
    private  Addapter addapter;
    ArrayList<User> users = new ArrayList<User>();
    ArrayList<User> users1 = new ArrayList<User>();



    final static  public   String   json2= "{'name': 'helloworld','array':[{'a':'111','b':'222','c':'333'},{'a':'111','b':'222','c':'333'},{'a':'111','b':'222','c':'333'},{'a':'111','b':'222','c':'333'},{'a':'111','b':'222','c':'333'},{'a':'111','b':'222','c':'333'},{'a':'999'}],'address':'111','people':{'name':'happ','sex':'girl'}}";
    public   String   json1= "{'name': 'helloworlda','array':[{'a':'111','b':'222','c':'333'},{'a':'111','b':'222','c':'333'},{'a':'111','b':'222','c':'333'},{'a':'111','b':'222','c':'333'},{'a':'111','b':'222','c':'333'},{'a':'111','b':'222','c':'333'},{'a':'111','b':'222','c':'333'},{'a':'111','b':'222','c':'333'},{'a':'111','b':'222','c':'333'},{'a':'111','b':'222','c':'333'},{'a':'111','b':'222','c':'333'},{'a':'111','b':'222','c':'333'},{'a':'111','b':'222','c':'333'},{'a':'111','b':'222','c':'333'},{'a':'111','b':'222','c':'333'},{'a':'111','b':'222','c':'333'},{'a':'111','b':'222','c':'333'},{'a':'111','b':'222','c':'333'},{'a':'111','b':'222','c':'333'},{'a':'111','b':'222','c':'333'},{'a':'111','b':'222','c':'333'},{'a':'111','b':'222','c':'333'},{'a':'zuihoudier','b':'222','c':'333'},{'a':'zuizhouyige'}],'address':'111','people':{'name':'happ','sex':'girl'}}";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);
        pullToRefreshView = (PullToRefreshView)findViewById(R.id.pulltorefresh);
        listView = (ListView)findViewById(R.id.main_list);

        JSONObject jsonObject = JsonUtil.convertJsonObj(json1);
        JSONArray jsonArray = JsonUtil.convertJsonArry(jsonObject,"array");
        users.clear();
        for (int i = 0;i<jsonArray.length();i++){
            JSONObject aaa = JsonUtil.convertJsonObj(jsonArray,i);
            String a = JsonUtil.getStr(aaa, "a");
            User user = new User();
            user.setAa(a);
            users.add(user);
        }


        addapter = new Addapter(this);
        addapter.setData(users);
        listView.setAdapter(addapter);

        pullToRefreshView.setFooter(true);
        pullToRefreshView.setOnHeaderRefreshListener(new PullToRefreshView.OnHeaderRefreshListener() {
            @Override
            public void onHeaderRefresh(PullToRefreshView view) {

                view.postDelayed(new Runnable() {
                    public void run() {
                        pullToRefreshView.onHeaderRefreshComplete();
                    }
                }, 5000);
            }
        });
        pullToRefreshView.setOnFooterRefreshListener(new PullToRefreshView.OnFooterRefreshListener() {
            @Override
            public void onFooterRefresh(PullToRefreshView view) {
                view.postDelayed(new Runnable() {
                    public void run() {
                        users1.clear();
                        JSONObject jsonObject = JsonUtil.convertJsonObj(json2);
                        JSONArray jsonArray = JsonUtil.convertJsonArry(jsonObject,"array");
                        for (int i = 0;i<jsonArray.length();i++){
                            JSONObject aaa = JsonUtil.convertJsonObj(jsonArray,i);
                            String a = JsonUtil.getStr(aaa, "a");
                            User user = new User();
                            user.setAa(a);
                            users1.add(user);
                        }
                        addapter.addData(users1);
                        pullToRefreshView.onFooterRefreshComplete();
                    }
                }, 5000);


            }
        });








    }
}
